/**
 * 
 */
package com.postek.coyote.postekwifi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.postek.cdf.CDFPTKAndroid;
import com.postek.coyote.postekwifi.bean.BBean;
import com.postek.coyote.postekwifi.bean.LabelBean;

import java.util.ArrayList;

/**
 * @author yrwang
 * 
 */
public class PrintBActivity extends AppCompatActivity {

	private CDFPTKAndroid cdf = MainActivity.cdf;
	private ArrayList<LabelBean> labelBeans = MainActivity.labelBeans;

	private EditText et_B1 = null;
	private EditText et_B2 = null;
	private Spinner sp_B3 = null;
	private Spinner sp_B4 = null;
	private EditText et_B5 = null;
	private EditText et_B6 = null;
	private EditText et_B7 = null;
	private Spinner sp_B8 = null;
	private EditText et_BS = null;
	private EditText et_print_number=null;
	private EditText et_print_cpnumber=null;

	private Button btnPrintB = null;
	private Button btnCancel = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.print_b_);

		et_B1 = (EditText) findViewById(R.id.et_print_B1);
		et_B2 = (EditText) findViewById(R.id.et_print_B2);
		sp_B3 = (Spinner) findViewById(R.id.sp_print_B3);
		sp_B4 = (Spinner) findViewById(R.id.sp_print_B4);
		et_B5 = (EditText) findViewById(R.id.et_print_B5);
		et_B6 = (EditText) findViewById(R.id.et_print_B6);
		et_B7 = (EditText) findViewById(R.id.et_print_B7);
		sp_B8 = (Spinner) findViewById(R.id.sp_print_B8);
		et_BS = (EditText) findViewById(R.id.et_print_BS);
		et_print_number= (EditText) findViewById(R.id.et_print_number);
		et_print_cpnumber= (EditText) findViewById(R.id.et_print_cpnumber);

		btnPrintB = (Button) findViewById(R.id.btn_printB_print);
		btnCancel = (Button) findViewById(R.id.btn_printB_cancel);

		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		btnPrintB.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int iB1 = 0;
				int iB2 = 0;
				int iB3 = 0;
				String iB4 = "";
				int iB5 = 0;
				int iB6 = 0;
				int iB7 = 0;
				char iB8 = 'N';
				String iBS = "";

				int number=Integer.parseInt(et_print_number.getText().toString());
				int cpnumber=Integer.parseInt(et_print_cpnumber.getText().toString());

				if (!("".equals(et_B1.getText().toString()))) {
					iB1 = Integer.parseInt(et_B1.getText().toString());
				}
				if (!("".equals(et_B2.getText().toString()))) {
					iB2 = Integer.parseInt(et_B2.getText().toString());
				}
				if (!("".equals(et_B5.getText().toString()))) {
					iB5 = Integer.parseInt(et_B5.getText().toString());
				}
				if (!("".equals(et_B6.getText().toString()))) {
					iB6 = Integer.parseInt(et_B6.getText().toString());
				}
				if (!("".equals(et_B7.getText().toString()))) {
					iB7 = Integer.parseInt(et_B7.getText().toString());
				}

				iB3 = sp_B3.getSelectedItemPosition();
				iB4 = ((String) sp_B4.getSelectedItem()).substring(0,
						((String) sp_B4.getSelectedItem()).indexOf(":"));
				iB8 = (((String) sp_B8.getSelectedItem()).toCharArray())[0];
				System.out.println(iB3 + iB4 + iB8);
				
				iBS = et_BS.getText().toString();

				int nReturn = cdf.PTK_DrawBarcode(iB1, iB2, iB3, iB4, iB5, iB6,
						iB7, iB8, iBS);

				if (nReturn == 0) {
					BBean bBean = new BBean(iB1, iB2, iB3, iB4, iB5, iB6,
							iB7, iB8, iBS);
					labelBeans.add(bBean);
					Toast.makeText(PrintBActivity.this, R.string.setting_success, Toast.LENGTH_SHORT).show();
				cdf.PTK_PrintLabel(number,cpnumber);
				} else {
					Toast.makeText(PrintBActivity.this,  R.string.setting_fail, Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
}
